package com.boang.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

import javax.swing.plaf.PanelUI;

/*
  @author: 13813
  @date: 2024/8/9 11:11
*/
@Configuration
@EnableMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class SpringSecurity {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

        //关闭跨站请求
        http.csrf().disable();

        http.authorizeHttpRequests().requestMatchers("/login", "/register", "/admin/addAdmin").permitAll()//放行
//                .requestMatchers("/spu/addSpu").hasRole("admin")
//                .requestMatchers("/spu/getSpuList").hasRole("baoan")
//                .requestMatchers("/spu/removeSpu").hasAuthority("prod")
                .anyRequest().authenticated();//其它请求都要认证

        http.formLogin().loginPage("/login") //跳转登录页的url
                .loginProcessingUrl("/logindo")//处理登录请求url
                .defaultSuccessUrl("/");//登录成功跳转url

        http.exceptionHandling().accessDeniedPage("/accessDenied");//跳转无权访问页面

        http.logout().logoutSuccessUrl("/login");//退出

        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
